#include <iostream>
#include "ImageManager.h"

sf::Image * ImageManager::requireImage(std::string const & filename)
{
    std::map<std::string, sf::Image*>::iterator it;

    if ((it = _images.find(filename)) == _images.end())
    {
        sf::Image * img = _imagePool.getNext();

        if (!(img->LoadFromFile("resource/" + filename + ".png")))
            return NULL;
        _images[filename] = img;
        _usesCount[filename] = 1;
        return img;
    }
    _usesCount[filename] += 1;
    return _images[filename];
}

void ImageManager::disposeOf(std::string const & filename)
{
    std::map<std::string, sf::Image*>::iterator it;

    if ((it = _images.find(filename)) == _images.end())
        return;
    if (--(_usesCount[filename]) == 0)
    {
        _imagePool.disposeOf((*it).second);
        _images.erase(it);
    }
}

vector ImageManager::getImgSize(std::string const & filename)
{
    sf::Image * img = requireImage(filename);
    return vector(img->GetWidth(), img->GetHeight());
}
